import * as fs from "fs";
let spawn = require('child_process').spawn;
let path = require('path');
// 读取文件夹里面的文件
let dir:string = "./audio/";
let newDir = "./wav/";
let fileArray: Array<string>  = [];

function m4a2wav(val){
    let newFile = path.parse(val);
    console.log("正在转码: << "+newFile.name+" >>");
    return new Promise((resolve, reject) => {
        let status = spawn('ffmpeg', [
            '-i',
            dir+val,
            newDir+newFile.name+".wav"
        ]);
        if(!status.error){
            resolve("<< "+newFile.name+" >>转码成功");
        } else {
            reject(status.error);
        }
    });
}

fileArray = fs.readdirSync(dir);
for (let i in fileArray) {
    let log = await m4a2wav(fileArray[i]);
    console.log(log);
}

